<?php
session_name(DicomScience);
session_start();


/*
 * The writeMedia controller listens to incoming files and information transferred via http_POST variables and
 * binary datastream. Return values are a XML coded datastream containing information about the success
 * storing the media.
 * 
 * The convention used in the FILE naming: "newfile" (without the quotes)
 */ 
 
// Get Config Data
require('../includes/gset.php');

// Defining response language
$session_language = $_SESSION['lang'];
if ($session_language == "") {
	
	// Assuming Controller has not been triggered by a PHP Session
	if ($lang == 1)	{
		require_once ('../includes/language/german.php');	
	} elseif($lang == 2){
		require_once ('../includes/language/english.php');
	} elseif($lang == 3){
		require_once ('../includes/language/french.php');
	}	
}

$datetime = time();

mkdir("$img_rep/$datetime", 0777);
mkdir("$thb_rep/$datetime", 0777);

$modality = $_POST['modality'];
$organ = $_POST['organ'];
$tags = $_POST['tags'];
$uploading_user = $_POST['user'];

# Determing MIME-Type
$mime = $_FILES['newfile']['type'];

# Setting relative FilePointers
$webImg = $img_store."/".$datetime."/".$_FILES['newfile']['name'];
$webThumb = $thb_store."/".$datetime."/".$_FILES['newfile']['name'];

# Setting File Names
$imgFile = $img_rep."/".$datetime."/".$_FILES['newfile']['name'];
$thbFile = $thb_rep."/".$datetime."/".$_FILES['newfile']['name'];

if ($mime == 'image/gif' || $mime == 'image/jpeg' || $mime == 'image/png' || $mime == 'image/tiff'){
	
	$newFileMime = '7';
	
	# Creating and storing image
	$image = new Imagick($_FILES['newfile']['tmp_name']);
	$image->setInterlaceScheme(Imagick::INTERLACE_NO);
	$image->writeImage($imgFile);
	$image->clear();
	$image->destroy();	
	
	# Creating and storing Thumbnail
	/*
	 * This is not like the perl interface does, but WTH - can be improved. See the perl script for the general
	 * usage creating thumbs (the right way).
	 */
	$thumb = new Imagick($_FILES['newfile']['tmp_name']);
	# $thumb->setGravity(Imagick::GRAVITY_CENTER);
	$thumb->thumbnailImage(50,60);
	# $thumb->setImageExtent(50,60);
	$thumb->writeImage($thbFile);
	$thumb->clear();
	$thumb->destroy();
	
	require('../includes/database/connect.php');
	$sql_storeImage = "INSERT INTO media SET tags='$tags', modality='$modality', organ='$organ', location_thumb='$webThumb', location_media='$webImg', medrep='$imgFile', imgrep='$thbFile', id_staff='9999', upload_date='$datetime', upload_user='9999', media_type='$newFileMime'";
	$storeImage = mysql_query($sql_storeImage);
	require('../includes/database/closedb.php');
	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
	echo "<newMedia>";
	echo "<status>";
	echo "success";
	echo "</status>";
	echo "</newMedia>";
	
	unlink($_FILES['newfile']['tmp_name']);
	
} elseif ($mime == 'video/mpeg' || $mime == 'video/quicktime' || $mime == 'video/x-msvideo') {
	$newFileMime = '1';
	
	$temp = $_FILES['newfile']['tmp_name'];
	move_uploaded_file($_FILES['newfile']['tmp_name'], $imgFile);
	chmod($imgFile, 0777);
	
	$bin = new Imagick('./lib/movie_background.png');
	
	# $pdf = new Imagick($temp);
	$bin->setImageFormat("png");
	$bin->thumbnailImage(50,60);	
	$thbFile = $thbFile.".png";
	
	$draw = new ImagickDraw();
	$draw->setFontSize( 10 );

	$bin->annotateImage($draw, 3, 50, 0, $_FILES['newfile']['name']);
	
	$bin->writeImage($thbFile);
	$bin->clear();
	$bin->destroy();

	require('../includes/database/connect.php');
	$sql_storeImage = "INSERT INTO media SET tags='$tags', modality='$modality', organ='$organ', location_thumb='$webThumb', location_media='$webImg', medrep='$imgFile', imgrep='$thbFile', id_staff='9999', upload_date='$datetime', upload_user='9999', media_type='$newFileMime'";
	$storeImage = mysql_query($sql_storeImage);
	require('../includes/database/closedb.php');

	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
	echo "<newMedia>";
	echo "<status>";
	echo "success";
	echo "</status>";
	echo "</newMedia>";

	
} elseif($mime == 'audio/x-wav' || $mime == 'audio/wav' || $mime == 'audio/x-mpeg' || $mime == 'audio/mpeg') {
$newFileMime = '5';
	
	$temp = $_FILES['newfile']['tmp_name'];
	move_uploaded_file($_FILES['newfile']['tmp_name'], $imgFile);
	chmod($imgFile, 0777);
	
	$bin = new Imagick('./lib/audio_background.png');
	
	# $pdf = new Imagick($temp);
	$bin->setImageFormat("png");
	$bin->thumbnailImage(50,60);	
	$thbFile = $thbFile.".png";
	
	$draw = new ImagickDraw();
	$draw->setFontSize( 10 );

	$bin->annotateImage($draw, 3, 50, 0, $_FILES['newfile']['name']);
	
	$bin->writeImage($thbFile);
	$bin->clear();
	$bin->destroy();

	require('../includes/database/connect.php');
	$sql_storeImage = "INSERT INTO media SET tags='$tags', modality='$modality', organ='$organ', location_thumb='$webThumb', location_media='$webImg', medrep='$imgFile', imgrep='$thbFile', id_staff='9999', upload_date='$datetime', upload_user='9999', media_type='$newFileMime'";
	$storeImage = mysql_query($sql_storeImage);
	require('../includes/database/closedb.php');

	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
	echo "<newMedia>";
	echo "<status>";
	echo "success";
	echo "</status>";
	echo "</newMedia>";

} elseif($mime == 'application/pdf'){

	$newFileMime = '6';

	$temp = $_FILES['newfile']['tmp_name'];
	move_uploaded_file($_FILES['newfile']['tmp_name'], $imgFile);
	chmod($imgFile, 0777);
	
	$pdf = new Imagick($imgFile);
	# $pdf = new Imagick($temp);
	$pdf->setImageFormat("png");
	$pdf->thumbnailImage(50,60);	
	$thbFile = $thbFile.".png";
	
	$draw = new ImagickDraw();
	$draw->setFontSize( 10 );

	$pdf->annotateImage($draw, 3, 50, 0, $_FILES['newfile']['name']);
	
	$pdf->writeImage($thbFile);
	$pdf->clear();
	$pdf->destroy();

	require('../includes/database/connect.php');
	$sql_storeImage = "INSERT INTO media SET tags='$tags', modality='$modality', organ='$organ', location_thumb='$webThumb', location_media='$webImg', medrep='$imgFile', imgrep='$thbFile', id_staff='9999', upload_date='$datetime', upload_user='9999', media_type='$newFileMime'";
	$storeImage = mysql_query($sql_storeImage);
	require('../includes/database/closedb.php');

	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
	echo "<newMedia>";
	echo "<status>";
	echo "success";
	echo "</status>";
	echo "</newMedia>";
	
} else{

	$newFileMime = '';
	// echo "<h1>".$_FILES['newfile']['type']."</h1>";
	$temp = $_FILES['newfile']['tmp_name'];
	move_uploaded_file($_FILES['newfile']['tmp_name'], $imgFile);
	chmod($imgFile, 0777);
	
	$bin = new Imagick('./lib/binary_background.png');
	
	# $pdf = new Imagick($temp);
	$bin->setImageFormat("png");
	$bin->thumbnailImage(50,60);	
	$thbFile = $thbFile.".png";
	
	$draw = new ImagickDraw();
	$draw->setFontSize( 10 );

	$bin->annotateImage($draw, 3, 50, 0, $_FILES['newfile']['name']);
	
	$bin->writeImage($thbFile);
	$bin->clear();
	$bin->destroy();

	require('../includes/database/connect.php');
	$sql_storeImage = "INSERT INTO media SET tags='$tags', modality='$modality', organ='$organ', location_thumb='$webThumb', location_media='$webImg', medrep='$imgFile', imgrep='$thbFile', id_staff='9999', upload_date='$datetime', upload_user='9999', media_type='$newFileMime'";
	$storeImage = mysql_query($sql_storeImage);
	require('../includes/database/closedb.php');

	header("Content-type: text/xml"); 
	echo "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>";
	echo "<newMedia>";
	echo "<status>";
	echo "success";
	echo "</status>";
	echo "</newMedia>";
	
}

# Delete Temporary File
# unlink($_FILES['newfile']['tmp_name']);

?>